<template>
  <div class="essay-container">
    <div class="title-container">
      <h1 class="title">
        用户管理
      </h1>
    </div>
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="data"
      label-width="100px"
      style="width:400px; margin-left:20px"
    >
      <el-form-item label="密码" prop="pass">
        <el-input v-model="data.pass"  type="password"/>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkpass">
        <el-input v-model="data.checkpass" type="password"/>
      </el-form-item>
    </el-form>
    <div style="margin-left:50px">
    <el-button type="primary" @click="updateData">
      提交
    </el-button>
    </div>
  </div>
</template>

<script>
import AdminService from '@/service/login';
import { removeToken } from '@/utils/token'
const adminService = new AdminService();
export default {
  name: "useradmin",
  data() {
    const validatePass = (rule, value, callback) => {
      if (!value) {
        callback(new Error("密码不允许为空"));
      } else {
        if (this.data.checkpass) {
          this.$refs.dataForm.validateField("请检查确认密码");
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.data.pass) {
        callback(new Error("两次密码不一致"));
      } else {
        callback();
      }
    };
    return {
      data: {
        pass: "",
        checkpass: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkpass: [{ validator: validatePass2, trigger: "blur" }]
      },
    };
  },
  methods:{
    updateData(){
      this.$refs.dataForm.validate((valid)=>{
        if(valid){
          adminService.loginUpdate({password:this.data.pass}).then(()=>{
            adminService.loginLogout().then(()=>{
              removeToken();
              this.$router.push({path:'/login'})
            })
            
          })
        }
      })
    }
  }
};
</script>

<style scoped>
.title-container {
  width: 100%;
  padding: 20px;
  font-weight: 500;
}
.title {
  padding-bottom: 10px;
  border-bottom: 1px solid #e5e5e5;
}
.essay-container {
  height: 100%;
  overflow: auto;
}
</style>